<?php


namespace App\Service;
class Auth
{
    /**
     * @递归
     * @user wsg
     * @method get
     * @data
     * @return \Illuminate\Http\Response
     */
    public static function getTree($data, $pid = 0,$html='--', $level=0)
    {
        static $result = [];
        //遍历节点信息数组 $data 中的每个元素
        foreach ($data as $item) {
            if ($item['pid'] == $pid) {
                $item['html'] = str_repeat($html, $level * 2);
                $item['level']= $level + 1;
                $result[] = $item;
                self::getTree($data, $item['id'],$html,$item['level']);
            }
        }
        return $result;
    }
    /**
     * @递归
     * @user wsg
     * @method get
     * @data 将数据的平面列表转换为分层树结构，以便于操作和显示
     * @return \Illuminate\Http\Response
     */
    public static function getTreeList($list)
    {
        //将每条数据中的id值作为其下标
        $temp = [];
        foreach($list as $v){
            $v['son'] = [];
            $temp[$v['id']] = $v;
        }
        //dd($temp);
        //获取分类树
        foreach($temp as $k=>$v){
            $temp[$v['pid']]['son'][] = & $temp[$v['id']];
        }
        return isset($temp[0]['son']) ? $temp[0]['son'] : [];
    }



















































    /**
     * 递归处理层级
     * @return void
     */
    public static function sonLevel($data, $pid = 0)
    {
        $arr = [];
        foreach ($data as $key => $val) {
            //等于0的时候代表是顶级菜单
            if ($val['pid'] == $pid) {
                //递归处理子集
                $val['son'] = self::sonLevel($data, $val['id']);
                $arr[] = $val;
            }
        }
        return $arr;
    }

}
